/*    */ package scripting.map;
/*    */ 
/*    */ import client.MapleClient;
/*    */ import configs.ServerConfig;
/*    */ import java.io.File;
/*    */ import java.io.FileReader;
/*    */ import java.io.IOException;
/*    */ import java.util.HashMap;
/*    */ import java.util.Map;
/*    */ import javax.script.Compilable;
/*    */ import javax.script.CompiledScript;
/*    */ import javax.script.Invocable;
/*    */ import javax.script.ScriptEngine;
/*    */ import javax.script.ScriptEngineFactory;
/*    */ import javax.script.ScriptEngineManager;
/*    */ import org.apache.logging.log4j.LogManager;
/*    */ import org.apache.logging.log4j.Logger;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class MapScriptManager
/*    */ {
/* 24 */   private static final Logger log = LogManager.getLogger(MapScriptManager.class.getName());
/* 25 */   private static final MapScriptManager instance = new MapScriptManager();
/* 26 */   private static final ScriptEngineFactory sef = (new ScriptEngineManager()).getEngineByName("nashorn").getFactory();
/* 27 */   private final Map<String, MapScript> scripts = new HashMap<>();
/*    */   
/*    */   public static synchronized MapScriptManager getInstance() {
/* 30 */     return instance;
/*    */   }
/*    */   
/*    */   public void getMapScript(MapleClient c, String scriptName, boolean firstUser) {
/* 34 */     if (this.scripts.containsKey(scriptName)) {
/* 35 */       ((MapScript)this.scripts.get(scriptName)).start(new MapScriptMethods(c));
/*    */       return;
/*    */     } 
/* 38 */     String type = "onUserEnter/";
/* 39 */     if (firstUser) {
/* 40 */       type = "onFirstUserEnter/";
/*    */     }
/* 42 */     File scriptFile = new File(ServerConfig.WORLD_SCRIPTSPATH + "/map/" + type + scriptName + ".js");
/* 43 */     if (!scriptFile.exists()) {
/* 44 */       if (c.getPlayer().isAdmin()) {
/* 45 */         c.getPlayer().dropMessage(5, "地图触发: 未找到 map/" + type + " 文件中的 " + scriptName + ".js 文件.");
/*    */       }
/* 47 */       log.error("地图触发: 未找到 map/" + type + " 文件中的 " + scriptName + ".js 文件. 在地图 " + c.getPlayer().getMapId() + " - " + c.getPlayer().getMap().getMapName());
/*    */       return;
/*    */     } 
/* 50 */     FileReader fr = null;
/* 51 */     ScriptEngine map = sef.getScriptEngine();
/*    */     try {
/* 53 */       fr = new FileReader(scriptFile);
/* 54 */       CompiledScript compiled = ((Compilable)map).compile(fr);
/* 55 */       compiled.eval();
/* 56 */     } catch (Exception e) {
/* 57 */       System.err.println("请检查(map/" + type + " 文件中的 " + scriptName + ".js)的文件." + e);
/* 58 */       log.error("请检查(map/" + type + " 文件中的 " + scriptName + ".js)的文件.", e);
/*    */     } finally {
/* 60 */       if (fr != null) {
/*    */         try {
/* 62 */           fr.close();
/* 63 */         } catch (IOException e) {
/* 64 */           System.err.println("ERROR CLOSING" + e);
/*    */         } 
/*    */       }
/*    */     } 
/* 68 */     if (c.getPlayer().isAdmin()) {
/* 69 */       c.getPlayer().dropMessage(5, "开始执行地图触发: map/" + type + " 文件中的 " + scriptName + ".js 文件.");
/*    */     }
/* 71 */     MapScript script = ((Invocable)map).<MapScript>getInterface(MapScript.class);
/* 72 */     this.scripts.put(scriptName, script);
/* 73 */     script.start(new MapScriptMethods(c));
/*    */   }
/*    */   
/*    */   public void clearScripts() {
/* 77 */     this.scripts.clear();
/*    */   }
/*    */ }


/* Location:              C:\PGX\Server.jar!\scripting\map\MapScriptManager.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */